//
//  VoiceFollowView.m
//  for_test
//
//  Created by Carlson Lee on 2017/4/5.
//  Copyright © 2017年 Carlson Lee. All rights reserved.
//  文字朗读

#import "VoiceFollowView.h"

@interface VoiceFollowView ()
{
    NSInteger _row;
    NSInteger _cloumn;
    
    UIColor* _color;
    UIColor* _cColor;
}

@property(nonatomic, strong)CAShapeLayer* shapeTip;

@property(nonatomic, strong)CAShapeLayer* mask;//遮罩
@property(nonatomic, strong)VoiceLabel* vLabel;
@property(nonatomic, strong)VoiceLabel* mLabel;

@end

@implementation VoiceFollowView

- (instancetype)initWithColor:(UIColor* )color changeColor:(UIColor* )cColor
{
    if(self = [super init]){
        [self setOpaque:NO];
        [self.layer setCornerRadius:5.0];
        [self.layer setMasksToBounds:YES];
        _color = color;
        _cColor = cColor;
    }
    return self;
}
//语音提示图标
- (CAShapeLayer *)shapeTip
{
    if(!_shapeTip){
        _shapeTip = [CAShapeLayer layer];
        [self.layer addSublayer:_shapeTip];
        [_shapeTip setLineWidth:2.0*ScaleX];
        [_shapeTip setLineCap:kCALineCapRound];
        [_shapeTip setBackgroundColor:[UIColor clearColor].CGColor];
        [_shapeTip setStrokeColor:UIColorFromRGB(0x8653c7).CGColor];
    }
    return _shapeTip;
}


- (CAShapeLayer *)mask
{
    if(!_mask){
        _mask = [CAShapeLayer layer];
        [_mask setFillColor:[UIColor blackColor].CGColor];
        [_mask setBackgroundColor:[UIColor clearColor].CGColor];
    }
    return _mask;
}

- (VoiceLabel *)mLabel
{
    if(!_mLabel){
        _mLabel = [[VoiceLabel alloc]init];
        [self addSubview:_mLabel];
    }
    return _mLabel;
}

- (VoiceLabel *)vLabel
{
    if(!_vLabel){
        _vLabel = [[VoiceLabel alloc]init];
        [self addSubview:_vLabel];
    }
    return _vLabel;
}

- (void)setVoiceStr:(NSString *)voiceStr
{
    _voiceStr = voiceStr;
    NSInteger ln = _voiceStr.length+1;
    _cloumn = floor(UIFrameW(self))/CELL_WIDTH;
    _row = ln/_cloumn+(ln%_cloumn?1:0);
    
    [self setNeedsDisplay];
    [self.vLabel drawVoice:_voiceStr coloumn:_cloumn color:_color];
    [self.mLabel drawVoice:_voiceStr coloumn:_cloumn color:_cColor];
}

- (void)setDuration:(NSInteger)duration
{
    _duration = duration;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.shapeTip.frame = CGRectMake(0, 0, CELL_WIDTH, CELL_WIDTH);
    UIBezierPath* bezier = [UIBezierPath bezierPath];
    CGFloat f_x = (CELL_WIDTH-14*ScaleX)/2;
    for(NSInteger i=0; i<3; i++){
        CGFloat f_y = (CELL_WIDTH-(14-i*4)*ScaleX)/2;
        CGPoint p1 = CGPointMake(f_x+6*i*ScaleX, f_y);
        CGPoint p2 = CGPointMake(f_x+6*i*ScaleX, f_y+(14-i*4)*ScaleX);
        [bezier moveToPoint:p1];
        [bezier addLineToPoint:p2];
    }
    self.shapeTip.path = bezier.CGPath;
    self.vLabel.frame = self.bounds;
    [self.vLabel setNeedsDisplay];
    
    /* mask path */
    UIBezierPath* mb = [UIBezierPath bezierPath];
    [mb moveToPoint:CGPointMake(0, 0)];
    [mb addLineToPoint:CGPointMake(UIFrameW(self)*_row, 0)];
    for(int i=0; i<_row; i++){
        [mb addLineToPoint:CGPointMake(UIFrameW(self)*(_row-i), CELL_WIDTH*(i+1))];
        [mb addLineToPoint:CGPointMake(UIFrameW(self)*(_row-i-1), CELL_WIDTH*(i+1))];
    }
    [mb closePath];
    self.mask.frame = CGRectMake(0, 0, UIFrameW(self)*_row, UIFrameH(self));
    self.mask.path = mb.CGPath;
    
    self.mLabel.frame = self.bounds;
    [self.mLabel setNeedsLayout];
    self.mLabel.layer.mask = self.mask;
    
    self.mask.position = CGPointMake(-UIFrameW(self)/2, UIFrameH(self)/2);
    
}

//画方格
- (void)drawCellWithContext:(CGContextRef )ctx
{
    CGContextSetLineWidth(ctx, .5);
    CGContextSetStrokeColorWithColor(ctx, UIColorFromRGB(0xe5e4e9).CGColor);
    for(NSInteger i=1; i<_row; i++){
        CGContextMoveToPoint(ctx, 0, CELL_WIDTH*i);
        CGContextAddLineToPoint(ctx, UIFrameW(self), CELL_WIDTH*i);
    }
    for(NSInteger i=1; i<_cloumn; i++){
        CGContextMoveToPoint(ctx, CELL_WIDTH*i, 0);
        CGContextAddLineToPoint(ctx, CELL_WIDTH*i, UIFrameH(self));
    }
    CGContextStrokePath(ctx);
}

//画字
- (void)drawWordWithContext:(CGContextRef )ctx
{
    NSInteger num = _voiceStr.length+1;
    for(NSInteger i=1; i<num; i++){
        NSString* word = [_voiceStr substringWithRange:NSMakeRange(i-1, 1)];
        CGSize ws = SDSize(word, SDFont(18, FONT_TYPE_SEMIBOLD), CGSizeZero);
        CGRect rt = CGRectMake(CELL_WIDTH*(i%_cloumn)+(CELL_WIDTH-ws.width)/2, CELL_WIDTH*(i/_cloumn)+(CELL_WIDTH-ws.height)/2, CELL_WIDTH, CELL_WIDTH);
        [word drawInRect:rt withAttributes:@{NSFontAttributeName:SDFont(18, FONT_TYPE_SEMIBOLD), NSForegroundColorAttributeName:UIColorFromRGB(0x3c3342)}];
    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawCellWithContext:context];
    
//    CGContextSaveGState(context);
//    CGContextSetBlendMode(context, kCGBlendModeDestinationOut);
//    [self drawWordWithContext:context];
//    CGContextRestoreGState(context);
}


@end


@interface VoiceLabel ()
{
    NSString* _voiceStr;
    NSInteger _cloumn;
    UIColor* _color;
}

@end

@implementation VoiceLabel

- (instancetype)init
{
    if(self = [super init]){
        
    }
    return self;
}

- (void)drawVoice:(NSString* )voice coloumn:(NSInteger )cloumn color:(UIColor* )color
{
    _voiceStr = voice;
    _cloumn = cloumn;
    _color = color;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
//    CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
    NSInteger num = _voiceStr.length+1;
    for(NSInteger i=1; i<num; i++){
        NSString* word = [_voiceStr substringWithRange:NSMakeRange(i-1, 1)];
        CGSize ws = SDSize(word, SDFont(18, FONT_TYPE_SEMIBOLD), CGSizeZero);
        CGRect rt = CGRectMake(CELL_WIDTH*(i%_cloumn)+(CELL_WIDTH-ws.width)/2, CELL_WIDTH*(i/_cloumn)+(CELL_WIDTH-ws.height)/2, CELL_WIDTH, CELL_WIDTH);
        
        [word drawInRect:rt withAttributes:@{NSFontAttributeName:SDFont(18, FONT_TYPE_SEMIBOLD), NSForegroundColorAttributeName:_color}];
        
    }
    CGContextRestoreGState(ctx);
}


@end
